﻿namespace IS.Projecto.WebService
{
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Web.Hosting;
    using System.Xml.Serialization;
    using Modelo;

    public class ProjectoService : IProjectoService
    {
        #region Fields

        private static IList<Docente> _listDocentes;
        private static IList<Sala> _listSalas;
        private static IList<Inscricao> _listInscricoes;
        private static IList<UnidadeCurricular> _listUnidadesCurriculares;
        private static IList<Horario> _listHorarios;
        private static IList<Curso> _listCursos;

        #endregion

        #region Properties

        protected static IList<Docente> ListDocentes
        {
            get
            {
                if (_listDocentes == null)
                {
                    var xmlPath = ConfigurationManager.AppSettings["XmlPath"] ?? "xml";
                    var filename = string.Format("{0}Docentes.xml", xmlPath);
                    _listDocentes = DeserializeList<Docente>("Docentes", filename);
                }
                return _listDocentes;
            }
        }

        protected static IList<Sala> ListSalas
        {
            get
            {
                if (_listSalas == null)
                {
                    var xmlPath = ConfigurationManager.AppSettings["XmlPath"] ?? "xml";
                    var filename = string.Format("{0}Salas.xml", xmlPath);
                    _listSalas = DeserializeList<Sala>("Salas", filename);
                }
                return _listSalas;
            }

        }

        protected static IList<Inscricao> ListInscricoes
        {
            get
            {
                if (_listInscricoes == null)
                {
                    var xmlPath = ConfigurationManager.AppSettings["XmlPath"] ?? "xml";
                    var filename = string.Format("{0}Inscricoes.xml", xmlPath);
                    _listInscricoes = DeserializeList<Inscricao>("Inscricoes", filename);
                }
                return _listInscricoes;
            }

        }

        protected static IList<UnidadeCurricular> ListUnidadesCurriculares
        {
            get
            {
                if (_listUnidadesCurriculares == null)
                {
                    var xmlPath = ConfigurationManager.AppSettings["XmlPath"] ?? "xml";
                    var filename = string.Format("{0}UnidadesCurriculares.xml", xmlPath);
                    _listUnidadesCurriculares = DeserializeList<UnidadeCurricular>("UnidadesCurriculares", filename);
                }
                return _listUnidadesCurriculares;
            }
        }

        protected static IList<Horario> ListHorarios
        {
            get
            {
                if (_listHorarios == null)
                {
                    var xmlPath = ConfigurationManager.AppSettings["XmlPath"] ?? "xml";
                    var filename = string.Format("{0}Horarios.xml", xmlPath);
                    _listHorarios = DeserializeList<Horario>("Horarios", filename);
                }
                return _listHorarios;
            }
        }

        protected static IList<Curso> ListCursos
        {
            get
            {
                if (_listCursos== null)
                {
                    var xmlPath = ConfigurationManager.AppSettings["XmlPath"] ?? "xml";
                    var filename = string.Format("{0}Cursos.xml", xmlPath);
                    _listCursos = DeserializeList<Curso>("Cursos", filename);
                }
                return _listCursos;
            }
        }

        #endregion

        #region Implementation of IProjectoService

        public IList<Docente> SelectAllDocentes()
        {
            return ListDocentes;
        }

        public Docente SelectDocente(int id)
        {
            return (from p in ListDocentes
                    where p.Id == id
                    select p).FirstOrDefault();
        }

        public IList<Sala> SelectAllSalas()
        {
            return ListSalas;
        }

        public Sala SelectSala(int id)
        {
            return (from s in ListSalas
                    where s.Id == id
                    select s).FirstOrDefault();
        }

        public IList<Inscricao> SelectInscricoes(int cursoId)
        {
            return (from p in ListInscricoes
                    where p.CursoId == cursoId
                    select p).ToList();
        }

        public Inscricao SelectInscricao(int id)
        {
            return (from i in ListInscricoes
                    where i.Id == id
                    select i).FirstOrDefault();
        }

        public IList<UnidadeCurricular> SelectAllUnidadesCurriculares()
        {
            return ListUnidadesCurriculares;
        }

        public UnidadeCurricular SelectUnidadeCurricular(int id)
        {
            return (from u in ListUnidadesCurriculares 
                    where u.Id == id
                    select u).FirstOrDefault();
        }

        public IList<Horario> SelectHorarios(int cursoId, DateTime start, DateTime end)
        {
            var ucs = from p in ListUnidadesCurriculares
                      where p.CursoId == cursoId
                      select p.Id;

            return (from p in ListHorarios
                    where ucs.Contains(p.UcId) && p.DataHora >= start && p.DataHora <= end
                   select p).ToList();
        }

        public Horario SelectHorario(int id)
        {
            return (from h in ListHorarios 
                    where h.Id == id
                    select h).FirstOrDefault();
        }

        public IList<Curso> SelectAllCursos()
        {
            return ListCursos;
        }

        public Curso SelectCurso(int id)
        {
            return (from c in ListCursos
                    where c.Id == id
                    select c).FirstOrDefault();
        }

        #endregion

        #region Members

        private static IList<T> DeserializeList<T>(string rootName, string filename)
        {
            Stream filestream = null;
            try
            {
                filename = Path.GetFullPath(Path.Combine(HostingEnvironment.MapPath("~"), filename));
                var root = new XmlRootAttribute(rootName)
                               {
                                   Namespace = "http://xml.is.estg.org/schema/DataSchema"
                               };
                var serializer = new XmlSerializer(typeof (List<T>), root);
                filestream = new FileStream(filename, FileMode.Open);
                var reader = new StreamReader(filestream);

                return (IList<T>) serializer.Deserialize(reader);
            }
            finally
            {
                if (filestream != null)
                    filestream.Close();  
            }
        }

        #endregion
    }
}